package com.haidechizi.mybatis.demo.web;


import com.haidechizi.mybatis.demo.Response;
import com.haidechizi.mybatis.demo.entity.User;
import com.haidechizi.mybatis.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("insertUser")
    public Response insertUser(@RequestBody User user) {
        try {
            user = userService.insertUser(user);
            return Response.builder().data(user).build();
        } catch (Exception e) {
            return Response.builder().data(user).build();
        }
    }

    @RequestMapping("updateUser")
    public Response updateUser(@RequestBody User user) {
        try {
            user = userService.updateUser(user);
            return Response.builder().data(user).build();
        } catch (Exception e) {
            return Response.builder().data(user).build();
        }
    }

    @RequestMapping("deleteUser")
    public Response deleteUser(@RequestBody User user) {
        try {
            userService.deleteUser(user);
            return Response.builder().data(user).build();
        } catch (Exception e) {
            return Response.builder().data(user).build();
        }
    }
}
